<?php
	include_once("../includes/functions.php");

	foreach ($_POST as $key=>$value)
	{
		switch ($key)
		{
			case "qstatement":
				if (trim($value) == "")
					array_push($errors,array($key,"Please type a question statement"));
				break;
		}
	}
	
	if (sizeof($errors) > 0)
		echo displayErrors($errors);
	elseif (isset($_POST["submit"]))
	{
		if ($_POST["submit"] == "Yes")
		{
			$groupid = ($_POST["qcategoryid"] == "2") ? 0 : $_POST["qgroupid"];

			if ($mode == "add")
			{
				$sql = "INSERT INTO question(qstatement, qcategoryid, qgroupid) VALUES('".$_POST["qstatement"]."','".$_POST["qcategoryid"]."','".$groupid."')";
				$addquestion = $conn1->Execute($sql);

				if (!$addquestion) {
					print("SQL statement failed with error:\n");
				} else {
					echo "Question Successfully Added! Click <a href=\"../question/viewquestions.php?a=setup\">here</a> to continue";
				}  
			}
			else
			{
				$sql = "UPDATE question SET qstatement = '".$_POST['qstatement']."', qcategoryid = '".$_POST['qcategoryid']."', qgroupid = '".$groupid."' WHERE id = '".$_GET['i']."'";
				$updatequestion = $conn1->Execute($sql);

				if (!$updatequestion) {
					print("SQL statement failed with error:\n");
				} else {
					echo "Question Successfully Updated! Click <a href=\"../question/viewquestions.php?a=setup\">here</a> to continue";
				}  
			}			
		}
		elseif ($_POST["submit"] == "Submit")
			echo displayConfirmation();
	}
	
?>